package com.xb.loan.admin.service.operatormanager.impl;

import com.xb.loan.admin.bean.system.OssOperator;
import com.xb.loan.admin.bean.system.OssOperatorRole;
import com.xb.loan.admin.dao.system.OssOperatorDao;
import com.xb.loan.admin.dao.system.OssOperatorRoleDao;
import com.xb.loan.admin.service.dict.IenumConstants;
import com.xb.loan.admin.service.operatormanager.OperatorManagerService;
import com.xb.loan.log.exception.BizException;
import com.xb.loan.util.cache.redis.comm.FormatOperation;
import com.xb.loan.util.mybatis.MyTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("operatorManagerService")
public class OperatorManagerServiceImpl implements OperatorManagerService {

	private final static String OPERA_ERROR = "操作失败!";
	private final static String PARAM_ERROR = "参数错误!";
	private final static String LOGIN_NAME_EXIST = "登录名已存在!";
	
	private int res = 0;
	
	@Autowired
	private OssOperatorDao ossOperatorDao;
	
	@Autowired
	private OssOperatorRoleDao ossOperatorRoleDao;
	
	@MyTransactional
	@Override
	public void saveOrUpdateOperator(String type, String roleId, OssOperator oo) throws BizException {
		OssOperatorRole oor = new OssOperatorRole(roleId);
		if (oo==null) {
			throw new BizException("",PARAM_ERROR);
		}
		OssOperator ossOperator = ossOperatorDao.queryOssOperatorByLoginName(oo.getLoginName());
		if ("save".equals(type)) {
			if (ossOperator!=null) {
				throw new BizException("",LOGIN_NAME_EXIST);
			}
			String operatorId = FormatOperation.assemblySeq(
					IenumConstants.PriKeyStrategy.OSS_OPERATOR.getCode(),
					IenumConstants.PriKeyStrategy.OSS_OPERATOR.getNum(),
					FormatOperation.formatDateToYMDHms(new Date()), "");
			oo.setOperatorId(operatorId);
			oo.setStatus(1);
			oo.setCreateTime(new Date());
			res = ossOperatorDao.insert(oo);
			if (res<1) {
				throw new BizException("",OPERA_ERROR);
			}
			oor.setOperatorId(operatorId);
			res = ossOperatorRoleDao.insert(oor);
			if (res<1) {
				throw new BizException("",OPERA_ERROR);
			}
		} else if ("update".equals(type)) {
			if (ossOperator!=null && !ossOperator.getOperatorId().equals(oo.getOperatorId())) {
				throw new BizException(LOGIN_NAME_EXIST);
			}
			res = ossOperatorDao.updateByPrimaryKeySelective(oo);
			if (res<1) {
				throw new BizException("",OPERA_ERROR);
			}
			if(!"10000".equals(oo.getOperatorId())){
				oor.setOperatorId(oo.getOperatorId());
				res = ossOperatorRoleDao.updateByPrimaryKey(oor);
				if (res<1) {
					throw new BizException("",OPERA_ERROR);
				}
			}
		} else {
			throw new BizException("",PARAM_ERROR);
		}
	}

	@Override
	public Map<String, Object> queryOssOperatorById(String operatorId) {
		return ossOperatorDao.queryOssOperatorById(operatorId);
	}
	@Override
	public List<OssOperator> queryOperatorList(int pageSize,int pageNum,String roleId){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pageSize", pageSize);
		map.put("pageNum", pageNum);
		map.put("roleId", roleId);
		return ossOperatorDao.queryOperatorList(map);
	}
	
	@Override
	public int queryOperatorCount(String roleId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("roleId", roleId);
		return ossOperatorDao.queryOperatorCount(map);
	}
	@Override
	@MyTransactional
	public void deleteOperator(String operatorId) throws BizException{
		res = ossOperatorDao.deleteByPrimaryKey(operatorId);
		if (res<1) {
			throw new BizException("",OPERA_ERROR);
		}
		res = ossOperatorRoleDao.deleteByPrimaryKey(operatorId);
		if (res<1) {
			throw new BizException("",OPERA_ERROR);
		}
	}
	@MyTransactional
	@Override
	public int updateLoginPassWord(String operatorId,String newPassword) throws BizException{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("operatorId", operatorId);
		map.put("newPassword", newPassword);
		res = ossOperatorDao.updateLoginPassWord(map);
		if (res<1) {
			throw new BizException(OPERA_ERROR);
		}
		return res;
	}
	@Override
	public List<OssOperator> queryAll(){
		return ossOperatorDao.queryAll();
	}

	@Override
	public OssOperator getOssOperatorById(String operatorId) {
		 return ossOperatorDao.selectByPrimaryKey(operatorId);
	}


}
